package com.zone.types.model;

import com.zone.types.enums.ResponseCode;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

/**
 * @author: zongzi
 * @description: 接口响应类
 * @date: 2024/6/17
 */
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class Response<T> implements Serializable {
    /** 响应码 */
    private String code;
    /** 描述 */
    private String info;
    /** 响应数据 */
    private T data;

    /**
     * 请求成功
     *
     * @param data 响应数据
     * @return 响应对象
     */
    public static <T> Response<T> success(T data) {
        Response<T> response = new Response<>();
        response.setCode(ResponseCode.SUCCESS.getCode());
        response.setInfo(ResponseCode.SUCCESS.getInfo());
        response.setData(data);
        return response;
    }

    /**
     * 请求失败
     *
     * @param data 响应数据
     * @return 响应对象
     */
    public static <T> Response<T> error(T data) {
        Response<T> response = new Response<>();
        response.setCode(ResponseCode.BIZ_ERROR.getCode());
        response.setInfo(ResponseCode.BIZ_ERROR.getInfo());
        response.setData(data);
        return response;
    }
}
